package com.bsj.power.pls.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.constant.NumericalConstants;
import com.bsj.power.common.def.vo.pls.humanface.IEventSubscriptionVO;
import com.bsj.power.pls.service.HumanFaceService;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.hikvision.artemis.sdk.constant.ContentType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author : zhuojie.c
 * @version V1.0
 * @Description: TODO
 * @date Date : 2024年09月18日
 */
@Slf4j
@Service
public class HumanFaceServiceImpl implements HumanFaceService {

    @Value("${hiKv.host}")
    private String host;

    @Value("${hiKv.appKey}")
    private String appKey;

    @Value("${hiKv.appSecret}")
    private String appSecret;

    /**
     * url格式：https://{hostname}:{port}/artemis/{uri}。
     */
    private final String ARTEMIS_PATH = "/artemis";

    /**
     * 按事件类型订阅事件
     *
     * @param eventSubscriptionVO
     * @return
     */
    @Override
    public JsonResult eventSubscriptionByEventTypes(@Valid IEventSubscriptionVO eventSubscriptionVO) {
        // STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
        ArtemisConfig.host = host;
        ArtemisConfig.appKey = appKey;
        ArtemisConfig.appSecret = appSecret;
        // 调用接口地址
        String api = "/api/eventService/v1/eventSubscriptionByEventTypes";
        String httpStr = "https://";
        // http默认端口为80  https默认端口为443
        if (host.contains("80")) {
            httpStr = "http://";
        }
        Map<String, String> path = Map.of(httpStr, ARTEMIS_PATH + api);
        String body = JSONObject.toJSONString(eventSubscriptionVO);
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, ContentType.CONTENT_TYPE_JSON);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            Integer code = jsonObject.getInteger("code");
            if (code != null && code.equals(NumericalConstants.ZERO)) {
                log.info("请求按事件类型订阅事件成功，请求地址：{}，请求参数{}，返回内容：{}", JSONObject.toJSONString(path), body, result);
                return JsonResult.success();
            } else {
                log.info("请求按事件类型订阅事件失败，请求地址：{}，请求参数{}，返回内容：{}", JSONObject.toJSONString(path), body, result);
                return JsonResult.fail();
            }
        } else {
            log.info("请求按事件类型订阅事件失败，返回无结果，请求地址：{}，请求参数{}", JSONObject.toJSONString(path), body);
            return JsonResult.fail();
        }
    }

    @Override
    public JsonResult eventUnSubscriptionByEventTypes(IEventSubscriptionVO eventSubscriptionVO) {
        // STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
        ArtemisConfig.host = host;
        ArtemisConfig.appKey = appKey;
        ArtemisConfig.appSecret = appSecret;
        // 调用接口地址
        String api = "/api/eventService/v1/eventUnSubscriptionByEventTypes";
        String httpStr = "https://";
        // http默认端口为80  https默认端口为443
        if (host.contains("80")) {
            httpStr = "http://";
        }
        Map<String, String> path = Map.of(httpStr, ARTEMIS_PATH + api);
        String body = JSONObject.toJSONString(eventSubscriptionVO);
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, ContentType.CONTENT_TYPE_JSON);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            Integer code = jsonObject.getInteger("code");
            if (code != null && code.equals(NumericalConstants.ZERO)) {
                log.info("请求按事件类型取消订阅成功，请求地址：{}，请求参数{}，返回内容：{}", JSONObject.toJSONString(path), body, result);
                return JsonResult.success();
            } else {
                log.info("请求按事件类型取消订阅失败，请求地址：{}，请求参数{}，返回内容：{}", JSONObject.toJSONString(path), body, result);
                return JsonResult.fail();
            }
        } else {
            log.info("请求按事件类型取消订阅失败，返回无结果，请求地址：{}，请求参数{}", JSONObject.toJSONString(path), body);
            return JsonResult.fail();
        }
    }

    @Override
    public JsonResult eventSubscriptionView() {
        // STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
        ArtemisConfig.host = host;
        ArtemisConfig.appKey = appKey;
        ArtemisConfig.appSecret = appSecret;
        // 调用接口地址
        String api = "/api/eventService/v1/eventSubscriptionView";
        String httpStr = "https://";
        // http默认端口为80  https默认端口为443
        if (host.contains("80")) {
            httpStr = "http://";
        }
        Map<String, String> path = Map.of(httpStr, ARTEMIS_PATH + api);
//        String body = JSONObject.toJSONString(eventTypes);
        String result = ArtemisHttpUtil.doPostStringArtemis(path, null, null, null, ContentType.CONTENT_TYPE_JSON);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            Integer code = jsonObject.getInteger("code");
            if (code != null && code.equals(NumericalConstants.ZERO)) {
                log.info("请求查询事件订阅信息成功，请求地址：{}，返回内容：{}", JSONObject.toJSONString(path), result);
                return JsonResult.success();
            } else {
                log.info("请求查询事件订阅信息失败，请求地址：{}，返回内容：{}", JSONObject.toJSONString(path), result);
                return JsonResult.fail();
            }
        } else {
            log.info("请求查询事件订阅信息失败，返回无结果，请求地址：{}", JSONObject.toJSONString(path));
            return JsonResult.fail();
        }
    }
}
